#!/bin/bash
set -e
if [ -z "${MountPoint}" ]; then
    EnvFile="../common/0_env.sh"
    # shellcheck source=../common/0_env.sh
    source ${EnvFile}
fi

echo -e "\n***********************************************************************"
echo -e "\t\t$0 : 测试 ORCAFS-1241"
echo -e "***********************************************************************"

tmp_dir="/bug_1241"
abs_tmp_dir="${MountPoint}${tmp_dir}"
SysUid=`cat /etc/passwd | grep ${SysUser} | awk -F: '{print $3}'`
SysGid=`cat /etc/passwd | grep ${SysUser} | awk -F: '{print $4}'`

echo -e "\n删除目录 ${tmp_dir} 的所有配额"
remove_all_quota_for_dir ${tmp_dir}
mkdir -p ${abs_tmp_dir}
orcafs-ctl --setpattern --storagepoolid=2 ${abs_tmp_dir} &> /dev/null
clean_dir ${abs_tmp_dir}
chown -R "${SysUid}":"${SysGid}" ${abs_tmp_dir}

echo -e "\n给目录 ${tmp_dir} 的创建配额"
limit=5
create_quota ${tmp_dir} "--uid=${SysUid}" "--hardnum=${limit}"
create_quota ${tmp_dir} "--gid=${SysGid}" "--hardsize=${limit}M"

su ${SysUser} << EOF
    abs_dir="${abs_tmp_dir}"
    for ((i=0;i<4;i++))
    do
        echo -e "\\t\${abs_dir}/file\${i}"
        dd if=/dev/zero of="\${abs_dir}/file\${i}" bs=1M count=1 &> /dev/null
    done
EOF

echo -e "\n查看目录 ${tmp_dir} 的配额"
orcafs-ctl --listquota | grep -E "Type|${tmp_dir}"

# check_limit
set +e
echo -e "\n测试 bug"
su -c "touch ${abs_tmp_dir}/filexx1" ${SysUser}
check_usedsize_and_usednum ${tmp_dir} "--uid=${SysUid}"
su -c "touch ${abs_tmp_dir}/filexx2" ${SysUser}
check_usedsize_and_usednum ${tmp_dir} "--uid=${SysUid}"
set -e

remove_quota_dir ${tmp_dir} "--uid=${SysUid}"
remove_quota_dir ${tmp_dir} "--gid=${SysGid}"
rm_abs_dir ${abs_tmp_dir}
